package com.foo.ajax;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.PropertyModel;

public abstract class LoginPanel extends Panel {
	private static final long serialVersionUID = 1L;
	private String username;
	private String password;
	private FeedbackPanel feedback;

	public LoginPanel(String id) {
		super(id);
		feedback = new FeedbackPanel("feedback");
		feedback.setOutputMarkupId(true);
		add(feedback);
		Form<Void> f = new Form<Void>("f");
		add(f);
		TextField<String> usernameBox = new TextField<String>("username",
				new PropertyModel<String>(this, "username"));
		usernameBox.setRequired(true);
		f.add(usernameBox);
		PasswordTextField passwordBox = new PasswordTextField("password",
				new PropertyModel<String>(this, "password"));
		passwordBox.setRequired(true);
		f.add(passwordBox);
		AjaxButton ok = new AjaxButton("ok") {
			
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				if (username.equals("u1") && password.equals("p1")) {
					onLoggedIn(target);
				} else {
					error("login failed");
					target.add(feedback);
				}
			}
			
			@Override
			protected void onError(AjaxRequestTarget target, Form<?> form) {
				target.add(feedback);
			}
		};
		f.add(ok);
	}

	abstract protected void onLoggedIn(AjaxRequestTarget target);

}
